package com.note7_11.question1;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class Server extends Thread{

	private Socket s;
	private Login l;
	
	public Server(Socket s,Login l){
		super();
		this.s = s;
		this.l = l;
	}
	
	public void run(){
		System.out.println("当前端口为："+s.getLocalPort());
		System.out.println("连接端口为："+s.getPort());
		try {
				InputStream in = s.getInputStream();
				ObjectInputStream oin = new ObjectInputStream(in);
				OutputStream out = s.getOutputStream();
				PrintWriter pw = new PrintWriter(out);
				User user = (User) oin.readObject();
				if(l.login(user)){
					pw.print("欢迎来到德莱联盟！召唤师："+user.getName());
				}else{
					pw.print("quit");
				}
				pw.flush();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(s != null){
				try {
					s.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
